import 'package:enterprise_v6_app/app/components/custom_modele/custom_row_title.dart';
import 'package:enterprise_v6_app/app/components/custom_modele/custom_title_content_view.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/closure/access_record_entity.dart';

/// 车辆-出入记录-详情
class AssessRecordCar extends StatefulWidget {
  const AssessRecordCar({required this.item, super.key});

  final AccessRecordEntity item;

  @override
  State<AssessRecordCar> createState() => _AssessRecordCarState();
}

class _AssessRecordCarState extends State<AssessRecordCar> {


  // 报警状态
  Widget _status(int i) {
    if (i == 0) {
      return Container(
        padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 8.w),
        decoration: BoxDecoration(
            color: const Color(0xFF5C6DFF),
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(8.r),
              bottomRight: Radius.circular(8.r),
              topRight: Radius.circular(8.r),
            )),
        child: Text('正常',
            style: TextStyle(
              fontSize: 10.sp,
              color: Colors.white,
            )),
      );
    }
    return Container(
      padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 8.w),
      decoration: BoxDecoration(
          color: const Color(0xFFFD6B6B),
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(8.r),
            bottomRight: Radius.circular(8.r),
            topRight: Radius.circular(8.r),
          )),
      child: Text('报警($i)',
          style: TextStyle(
            fontSize: 10.sp,
            color: Colors.white,
          )),
    );
  }

  // 查询查询出入记录
  Future<List<AccessRecordEntity>> checkAssetsDetails(int pageNum,int pageSize, int type, {String? search}) async {
    List<AccessRecordEntity> list = [];
    final res = await HttpUtil().generateGetRequest<List<AccessRecordEntity>>(Interface.getRecordPage,queryParameters: {
      'pageNum': pageNum,
      'pageSize': pageSize,
      'type': type , // 1:人员 0:车辆
      'carNumber': widget.item.licensePlate,// 车牌号
    }).execute();

    res.onSuccess((result) {
      if(result.data != null) {
        list = result.data!;
      }
    });
    return list;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(
        left: AppDimens.dp16,
        right: AppDimens.dp16,
        top: AppDimens.dp16,
      ),
      child: CustomEasyRefreshList(
        dataLoad: (pageNum, pageSize, {search}) async {
          return checkAssetsDetails(pageNum, pageSize, widget.item.recordType!, search: search);
        },
        itemBuilder: (item, index) {
          return CustomTitleContentView(
            title: Row(
              children: [
                SvgPicture.asset(
                  AppImages.icon_title,
                  width: 20.w,
                  height: 20.w,
                ),
                AppGaps.hGap6,
                Text(
                  '车牌${item.licensePlate}',
                  style: TextStyle(color: const Color(0xff333333), height: 1.2, fontWeight: FontWeight.w500, fontSize: 15.sp),
                ),
                const Spacer(),
                _status(index),
              ],
            ),
            content: Column(
              children: [
                AppGaps.vGap6,
                Container(
                  width: double.infinity,
                  padding: EdgeInsets.only(left: 12.w, right: 12.w, top: 6.w, bottom: 6.w),
                  decoration: BoxDecoration(
                    color: const Color(0xFFF7F7FB),
                    borderRadius: BorderRadius.circular(4.w),
                  ),
                  child: Text(
                    '${item.startDate} 至 ${item.endDate}',
                    style: TextStyle(
                      color: const Color(0xFF5C6DFF),
                      fontSize: 14.sp,
                    ),
                  ),
                ),
                CustomRowTitle(
                  title: '驾驶员姓名',
                  content: '${item.name}',
                ),
                CustomRowTitle(
                  title: '运输介质',
                  content: '${item.carTypeName}',
                ),
                CustomRowTitle(
                  title: '联系电话',
                  content: '${item.visitorTelephone}',
                ),
                CustomRowTitle(
                  title: '来访事由',
                  content: '${item.reasonForVisit}',
                ),
              ],
            ),
          ).click(() {
            // Get.toNamed(AppRoutes.accessRecordDetailsPersonnelList);
          });
        },
      ),
    );
  }

}
